//
//  ViewController.m
//  12-24Veb
//
//  Created by blue on 15-12-24.
//  Copyright (c) 2015年 hyd. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIWebViewDelegate,UITextFieldDelegate>
{
    UIWebView * web;
    UIActivityIndicatorView * av;
    UITextField * tf;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    
    web = [[UIWebView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:web];
    web.delegate =  self;
    
    tf = [[UITextField alloc]initWithFrame:CGRectMake(15, 0, self.view.frame.size.width -30, 44)];
    tf.delegate = self;
//   tf.layer.borderWidth = 1;
//   tf.layer.cornerRadius = 4;
    tf.returnKeyType = UIReturnKeyGo;
    [self.navigationController.navigationBar addSubview:tf];
    
    
    av = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [av sizeToFit];
    av.center = CGPointMake(self.view.frame.size.width/2.0, self.view.frame.size.height/2.0);
    [self.view addSubview:av];
    NSURL * url = [NSURL URLWithString:@"http://localhost"];
    NSURLRequest * request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
    [web loadRequest:request ];
    
    self.navigationController.toolbarHidden = NO;
    UIBarButtonItem * back = [[UIBarButtonItem alloc]initWithTitle:@"后退" style:UIBarButtonItemStylePlain target:self action:@selector(goBack:)];
    UIBarButtonItem * forward = [[UIBarButtonItem alloc]initWithTitle:@"前进" style:UIBarButtonItemStylePlain target:self action:@selector(goForward:)];
    UIBarButtonItem * refresh = [[UIBarButtonItem alloc]initWithTitle:@"刷新" style:UIBarButtonItemStylePlain target:self action:@selector(goFresh:)];
    self.toolbarItems = @[back,forward,refresh];

    
}

- (void)goBack:(id)sender{

    if ([web canGoBack]) {
        [web goBack];
    }


}

- (void)goForward:(id)sender{
    
    if ([web canGoForward]) {
        [web goForward];
    }
    
    
}
- (void)goFresh:(id)sender{
    
    [web reload];
}





- (BOOL)textFieldShouldReturn:(UITextField *)textField

{
    textField.text = textField.text.lowercaseString;
    NSString * urlString = textField.text;
    if (![urlString hasPrefix:@"http://"]) {
        urlString = [@"http://" stringByAppendingString:urlString];
    }
    NSURL * url = [NSURL URLWithString:@"http://localhost"];
    NSURLRequest * request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
    [web loadRequest:request ];
    return YES;


}
- (void)webViewDidStartLoad:(UIWebView *)webView
{

    [av startAnimating];

}


- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [av stopAnimating];

}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{

    [av startAnimating];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
